var pageNum=1,pageSize=5,pageTotal=0;
var pageNum2=1,pageSize2=5,pageTotal2=0;
var pageNum3=1,pageSize3=5,pageTotal3=0;
$(function(){
	loadAdminMessage();
	
	$("#alterBut").click(function(){
		$("#alterDiv").hide();
	});
});

function showAdminMsgTable(){
	$("#adminMsgTable").show();
	$("#yingPinMsgTable").hide();
	$("#myGoMsgTable").hide();
	$("#top-font").text("查看管理员消息");
	loadAdminMessage();
}

function loadAdminMessage(){
	utilAjax("/adminRec/queryAllAdminRec_news_status","pageNum="+pageNum+"&pageSize="+pageSize,"get",
			function(data){
		pageNum = data.pageNum;
		pageSize = data.pageSize;
		pageTotal = data.pages;
		parseAdminMessage(data.list);
	});
}
function parseAdminMessage(lists){
	var adminCon = "";
	if(lists!=""){
		adminCon +="<tr class='info text-info'>"+
					"<th>编号</th>"+
					"<th>管理员</th><th>信息内容</th>"+
					"<th>信息状态</th><th>操作</th>"+
					"</tr>";
		$.each(lists,function(i,a){
			adminCon+="<tr>" +
						"<td>"+a.adminRec_news_id+"</td>" +
						"<td>"+a.adminMsg.admin_id+" 号管理员</td>" +
						"<td><textarea class='form-control' rows='2'>"+a.adminRec_news_context+"</textarea></td>" +
						"<td>"+(a.adminRec_news_status==1?"未读":"已读")+"</td>" +
						"<td>" +
							"<input type='button' value='已读' onclick=updateAdminMessageState("+a.adminRec_news_id+",2) class='btn btn-primary'> " +
							"<input type='button' value='删除' onclick=updateAdminMessageState("+a.adminRec_news_id+",3) class='btn btn-primary'> " +
						"</td>" +
					"</tr>";
		})
		adminCon+= "<tr><td colspan='5' align='center'>当前页:"+pageNum+"/总页数:"+pageTotal+"</td></tr>";
		adminCon+= "<tr>" +
				"<td colspan='5'>" +
				"<input type='button' value='首页' onclick=changeAdminMessagePage(this) class='btn btn-primary'> " +
				"<input type='button' value='上一页' onclick=changeAdminMessagePage(this) class='btn btn-primary'> " +
				"<input type='button' value='下一页' onclick=changeAdminMessagePage(this) class='btn btn-primary'> " +
				"<input type='button' value='末页' onclick=changeAdminMessagePage(this) class='btn btn-primary'> " +
			"</td></tr>";
	}else{
		adminCon+= "<tr><td width='1400' align='center'><font size='6' color='red'>暂无消息</font></td></tr>";
	}
	$("#adminMsgTable").html(adminCon);
}
function changeAdminMessagePage(op){
	var butCon = $(op).val();
	switch(butCon){
	case "首页":
		pageNum=1;
		loadAdminMessage();
		break;
	case "上一页":
		if(pageNum>1){
			pageNum--;
			loadAdminMessage();
		}	
		break;
	case "下一页":
		if(pageNum<pageTotal){
			pageNum++;
			loadAdminMessage();
		}
		break;
	case "末页":
		pageNum=pageTotal;
		loadAdminMessage();
		break;
	}
}
function updateAdminMessageState(adminRec_news_id,adminRec_news_status){
	utilAjax("/adminRec/updateAdminRecRec_news_status",
			"adminRec_news_id="+adminRec_news_id+"&adminRec_news_status="+adminRec_news_status,
			"post",
			function(flag){
		if(flag){
			$("#alterContent").text("操作成功");
			loadAdminMessage();
		}else{
			$("#alterContent").text("操作操作失败");
		}
		loadAlter();
	});
}
//查看招聘信息
function showYingPinMsgTable(){
	$("#adminMsgTable").hide();
	$("#yingPinMsgTable").show();
	$("#myGoMsgTable").hide();
	$("#top-font").text("查看招聘者消息");
	loadZhaoPinMessage();
}
function loadZhaoPinMessage(){
	utilAjax("/my_news/queryAllNews_visit_stateMy_news","pageNum="+pageNum2+"&pageSize="+pageSize2,"get",
			function(data){
		pageNum2 = data.pageNum;
		pageSize2 = data.pageSize;
		pageTotal2 = data.pages;
		parseZhaoPinMessage(data.list);
	});
}
function parseZhaoPinMessage(lists){
	var yingPinZhe = "";
	if(lists!=""){
		yingPinZhe +="<tr class='info text-info'>"+
					"<th>编号</th>"+
					"<th>求聘者</th><th>信息内容</th>"+
					"<th>信息状态</th><th>操作</th>"+
					"</tr>";
		$.each(lists,function(i,z){
			yingPinZhe+="<tr>" +
						"<td>"+z.news_id+"</td>" +
						"<td>"+z.basicInfor.basic_name+"</td>" +
						"<td><textarea class='form-control' rows='2'>"+z.news_context+"</textarea></td>" +
						"<td>"+(z.news_visit_state==1?"未读":"已读")+"</td>" +
						"<td>" +
							"<input type='button' value='已读' onclick=updateYingPinMessageState("+z.news_id+",2) class='btn btn-primary'> " +
							"<input type='button' value='删除' onclick=updateYingPinMessageState("+z.news_id+",3) class='btn btn-primary'> " +
						"</td>" +
					"</tr>";
		})
		yingPinZhe+= "<tr><td colspan='5' align='center'>当前页:"+pageNum2+"/总页数:"+pageTotal2+"</td></tr>";
		yingPinZhe+= "<tr>" +
				"<td colspan='5'>" +
				"<input type='button' value='首页' onclick=changeYingPinMessagePage(this) class='btn btn-primary'> " +
				"<input type='button' value='上一页' onclick=changeYingPinMessagePage(this) class='btn btn-primary'> " +
				"<input type='button' value='下一页' onclick=changeYingPinMessagePage(this) class='btn btn-primary'> " +
				"<input type='button' value='末页' onclick=changeYingPinMessagePage(this) class='btn btn-primary'> " +
			"</td></tr>";
	}else{
		yingPinZhe+= "<tr><td width='1400' align='center'><font size='6' color='red'>暂无消息</font></td></tr>";
	}
	$("#yingPinMsgTable").html(yingPinZhe);
}
function changeYingPinMessagePage(op){
	var butCon = $(op).val();
	switch(butCon){
	case "首页":
		pageNum2=1;
		loadZhaoPinMessage();
		break;
	case "上一页":
		if(pageNum2>1){
			pageNum2--;
			loadZhaoPinMessage();
		}	
		break;
	case "下一页":
		if(pageNum2<pageTotal2){
			pageNum2++;
			loadZhaoPinMessage();
		}
		break;
	case "末页":
		pageNum2=pageTotal2;
		loadZhaoPinMessage();
		break;
	}
}
function updateYingPinMessageState(news_id,news_visit_state){
	utilAjax("/my_news/updateNews_visit_state",
			"news_id="+news_id+"&news_visit_state="+news_visit_state,
			"post",
			function(flag){
		if(flag){
			$("#alterContent").text("操作成功");
			loadZhaoPinMessage();
		}else{
			$("#alterContent").text("操作操作失败");
		}
		loadAlter();
		
	});
}
//显示我的信息
function showMyGoMsgTable(){
	$("#adminMsgTable").hide();
	$("#yingPinMsgTable").hide();
	$("#myGoMsgTable").show();
	$("#top-font").text("查看我发送的消息");
	loadMyGoMessage();
}
function loadMyGoMessage(){
	utilAjax("/rec_news/queryAllRec_news_status","pageNum="+pageNum3+"&pageSize="+pageSize3,"get",
			function(data){
		pageNum3 = data.pageNum;
		pageSize3 = data.pageSize;
		pageTotal3 = data.pages;
		parseMyGoMessage(data.list);
	});
}

function parseMyGoMessage(lists){
	var MyCon = "";
	if(lists!=""){
		MyCon +="<tr class='info text-info'>"+
					"<th>编号</th>"+
					"<th>求聘者</th><th>信息内容</th>"+
					"<th>信息状态</th><th>操作</th>"+
					"</tr>";
		$.each(lists,function(i,m){
			MyCon+="<tr>" +
						"<td>"+m.rec_news_id+"</td>" +
						"<td>"+m.basicInfor.basic_name+"</td>" +
						"<td><textarea class='form-control' rows='2'>"+m.rec_news_context+"</textarea></td>" +
						"<td>"+(m.rec_news_visit_state==1?"未读":"已读")+"</td>" +
						"<td>" +
							"<input type='button' value='已读' onclick=updatemyGoMessageState("+m.rec_news_id+",2) class='btn btn-primary'> " +
							"<input type='button' value='删除' onclick=updatemyGoMessageState("+m.rec_news_id+",3) class='btn btn-primary'> " +
						"</td>" +
					"</tr>";
		})
		MyCon+= "<tr><td colspan='5' align='center'>当前页:"+pageNum3+"/总页数:"+pageTotal3+"</td></tr>";
		MyCon+= "<tr>" +
				"<td colspan='5'>" +
				"<input type='button' value='首页' onclick=changemyGoMessagePage(this) class='btn btn-primary'> " +
				"<input type='button' value='上一页' onclick=changemyGoMessagePage(this) class='btn btn-primary'> " +
				"<input type='button' value='下一页' onclick=changemyGoMessagePage(this) class='btn btn-primary'> " +
				"<input type='button' value='末页' onclick=changemyGoMessagePage(this) class='btn btn-primary'> " +
			"</td></tr>";
	}else{
		MyCon+= "<tr><td width='1400' align='center'><font size='6' color='red'>暂无消息</font></td></tr>";
	}
	$("#myGoMsgTable").html(MyCon);
}
function updatemyGoMessageState(rec_news_id,rec_news_visit_state){
	utilAjax("/rec_news/updateRec_news_visit_state",
			"rec_news_id="+rec_news_id+"&rec_news_visit_state="+rec_news_visit_state,
			"post",
			function(flag){
		if(flag){
			$("#alterContent").text("操作成功");
			loadMyGoMessage();
		}else{
			$("#alterContent").text("操作操作失败");
		}
		loadAlter();
		
	});
}

function changemyGoMessagePage(op){
	var butCon = $(op).val();
	switch(butCon){
	case "首页":
		pageNum3=1;
		loadMyGoMessage();
		break;
	case "上一页":
		if(pageNum3>1){
			pageNum3--;
			loadMyGoMessage();
		}	
		break;
	case "下一页":
		if(pageNum3<pageTotal3){
			pageNum3++;
			loadMyGoMessage();
		}
		break;
	case "末页":
		pageNum3=pageTotal3;
		loadMyGoMessage();
		break;
	}
}


function utilAjax(url,data,type,success){
	$.ajax({
		url:url,
		data:data,
		dataType:'json',
		type:type,
		success:success,
		error:function(){
		}
	});
}

function loadAlter(){
	var alterCon = $("#alterContent").text();
	if(alterCon!=""){
		$(".alterDiv").show();
		var interval = setInterval(function(){
			$(".alterDiv").hide();
			$("#alterContent").text("");
			clearInterval(interval);
		},3000);
	}
	$("#alterBut").click(function(){
		$(".alterDiv").hide();
	});
}